<template>
	<view class="container" style="padding-bottom: 220rpx;">
		<itemBaseInfo :itemModel='itemModel' @shareAction='shareAction'></itemBaseInfo>
		<view @click="payClick" style="display: flex;flex-direction: row;align-items: center;justify-content: space-between;background-color: #FFF;margin-top: 10rpx;height: 88rpx;padding: 0 20rpx;">
			<view style="display: flex;flex-direction: row;align-items: center;justify-content: space-between;">
				<view style="font-size: 30rpx;">选择</view>
				<view style="margin-left: 40rpx;color: #999;font-size: 26rpx;">规格</view>
			</view>
			<u-icon name="arrow-right" color="#999" size="15"></u-icon>
		</view>
		<itemDetail :itemModel='itemModel'></itemDetail>
		<!-- 规格选择弹窗 -->
		<itemspecPopup ref="itemspecPopup" :itemModel='itemModel' @skuSpecClick='skuSpecClick' @confirm='skuSpecConfirm' @joinShopCartSuccess='joinShopCartSuccess' @close='itemspecPopupClose' @open='itemspecPopupOpen'></itemspecPopup>
		<view class="" style="position: fixed;bottom: 0;width: 750rpx;height: 140rpx;background: #FFF;padding-top: 16rpx">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @buttonClick="goodsButtonClick"></uni-goods-nav>
		</view>
	</view>
</template>

<script>
	import itemBaseInfo from '@/pageA/merchant/components/itemBaseInfo.vue'
	import itemDetail from '@/pageA/merchant/components/itemDetail.vue'
	import itemspecPopup from '@/pageA/merchant/components/itemspecPopup/itemspecPopup.vue'
	import {stockCart} from '@/api/stockCart.js'
	import {
		merchant
	} from '@/api/merchant.js'
	import {
		activity
	} from '@/api/activity.js'
	import {
		common
	} from "@/api/common.js";
	import { findSpecList, getSpecList_sel, getSpecs_sel, goCreateOrderParams } from './function.js'
	export default{
		components: {itemBaseInfo, itemDetail, itemspecPopup},
		data() {
			return {
				shopCartScene: 'generay',
				itemModel: {},
				activityId: '',
				activityModel: {},
				options: [{
							icon: 'headphones',
							text: '客服',
							key: 'service'
						}, {
							icon: 'shop',
							text: '店铺',
							info: 0,
							infoBackgroundColor:'#007aff',
							infoColor:"red",
							key: 'shop',
						},
						{
							icon: 'cart',
							text: '购物车',
							info: 0,
							hidden: false,
							key: 'shopCart'
						}],
					    buttonGroup: [{
					      text: '加入购物车',
					      backgroundColor: '#8b5bfd',
					      color: '#fff',
						  hidden: false,
						  key: 'joinShopCart'
					    },
					    {
					      text: '立即购买',
					      backgroundColor: '#FC5E53',
					      color: '#fff',
						  key: 'pay'
					    }
					]
			}
		},
		onLoad() {
			this.obj_id = this.$Route.query.obj_id ||this.$Route.query.itemId
			if(this.$Route.query.shopCartScene){
				this.shopCartScene = this.$Route.query.shopCartScene
			}
			uni.showShareMenu({
				withShareTicket:true,
				//设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
				menus:["shareAppMessage","shareTimeline"]
			})
		},
		onShow() {
			this.getItemInfo()
			this.getShopCartCount()
		},
		onShareAppMessage(res) {
			return {
				title: this.itemModel.name,
				path: 'pages/home/home?CHN=defaule' + '&parentCode=' + this.$storage.getStorageDataSync(this.$store.state.userInfo).code + '&routerName=itemDetail&obj_id=' + this.obj_id,
				imageUrl: this.itemModel.coverUrl,
			}
		},
		methods:{
			shareAction(){

			},
			goodsButtonClick(val){
				switch (val.content.key){
					case 'shop':
						if(this.itemModel.shop){
							this.$Router.push({
								name: 'shopDetail',
								params: {
									shopId: this.itemModel.shop
								}
							})
						}
						break;
					case 'shopCart':
						if(this.shopCartScene == 'giftSection'){
							this.$Router.push({
								name: 'shopCartForGift',
								params: {}
							})
						}else{
							this.$Router.push({
								name: 'shopCart',
								params: {}
							})
						}
						
						break;
					case 'joinShopCart':
						this.payClick()
						break;
					case 'pay':
						this.payClick()
						break;
					default:
						break;
				}
			},
			joinShopCartSuccess(){
				this.$refs.itemspecPopup.close()
				this.getShopCartCount()
			},
			getShopCartCount(){
				common.operateActionPost('/merchant/shopCart/getShopCartCount/', {scene: this.shopCartScene}).then(res => {
					if(res.data.code == 10000){
						var totalNum = res.data.data.count
						this.options[2].info = totalNum
					}
				})
			},
			payClick(){
				var specList = findSpecList(this.itemModel.skuList)
				this.$set(this.itemModel, 'specList', specList)
				// 默认点击第一个
				this.itemModel.specList.forEach((item, index)=>{
					this.skuSpecClick('', index, 0)
				})
				this.$refs.itemspecPopup.show(this.shopCartScene)
			},
			//获取item信息
			getItemInfo() {
				merchant.getItemInfo({obj_id: this.obj_id}).then(res => {
					if(res.data.code == 10000){
						this.itemModel = res.data.data
						if(this.itemModel.activity){
							this.activityId = this.itemModel.activity
							this.getActivityInfo()
						}
					}
				})
			},
			getActivityInfo() {
				activity.getItemById({obj_id: this.activityId}).then(res => {
					if(res.data.code == 10000){
						this.activityModel = res.data.data
					}
				})
			},
			goShop(){
				// this.$Router.push({
				// 	name: 'storeForTakeout',
				// 	params: {
				// 		storeId: this.itemModel.shop
				// 	}
				// })
			},
			goCreateOrder(){
				// console.log('去下单', this.itemModel)
				// 添加购物车商品
				var params = goCreateOrderParams(this.itemModel)
				this.$Router.push({
					name: 'createOrder',
					params: params
				})
			},
			
			// 弹窗立即支付点击事件
			skuSpecConfirm(e){
				for(var spec of this.itemModel.specList){
					var flag = 0
					this.itemModel.specList_sel = this.itemModel.specList_sel?this.itemModel.specList_sel:[]
					for(var spec_sel of this.itemModel.specList_sel){
						if(spec.specName == spec_sel.specName){
							flag = 1
						}
					}
					if(flag==0){
						this.$show.showMessage('请选择'+spec.specName)
						return
					}
				}
				this.itemModel.payCount = e
				this.goCreateOrder()
				this.$refs.itemspecPopup.close()
			},
			skuSpecClick(spec, index, index2){
				this.setSpecStatus(spec, index, index2)
				this.itemModel.specList_sel = getSpecList_sel(this.itemModel.specList)
				this.itemModel.specs_sel = getSpecs_sel(this.itemModel.specList_sel)
				this.getSkuModel()
				this.$forceUpdate()
			},
			itemspecPopupClose(){
				
			},
			itemspecPopupOpen(){
			},
			setSpecStatus(spec, index, index2){
				//先把当前的组的所有select设false
				var specList = this.itemModel.specList
				var specValueList = specList[index].specValueList
				for (var i in specValueList) {
					var item = specValueList[i]
					if (index2 == i) {
						item.select = true
					} else {
						item.select = false
					}
				}
			},
			//查找选中的sku
			getSkuModel(){
				var specValues = JSON.stringify(this.itemModel.specList_sel)
				for(var sku of this.itemModel.skuList){
					if(sku.specValues == specValues){
						if(sku.cover){
							this.itemModel.coverUrl = sku.coverUrl
						}
						this.itemModel.priceModel = sku.priceModel
						// this.itemModel.priceModel.costPrice = sku.costPrice
						// this.itemModel.priceModel.sellingPrice = sku.sellingPrice
						// this.itemModel.priceModel.incomesPrice = sku.incomesPrice
						// this.itemModel.priceModel.realPay = sku.realPay
						this.itemModel.stock = sku.stock
						this.itemModel.skuId = sku.id
						this.itemModel.specValues = sku.specValues
					}
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	
</style>
